version 16
quietly{
use "C:\Users\achen.POL-PF10YAEL\Documents\Word\citizen assemblies\Krashinsky project\Ontario data\Stephenson data\Stephenson ON survey data.dta"

* USE THIS ONE FOR LAPTOP
*cd "C:\Users\achen.POL-PF10YAEL\Documents\Word\citizen assemblies\Krashinsky project\Ontario data\Stephenson data"

*THIS ONE IS FOR WORK DESKTOP
cd "C:\Users\achen\Documents\Word\citizen assemblies\Krashinsky project\Ontario data\Stephenson data"

*IS THIS THE LOG FILE YOU WANT?
log using stephensonontariofinalrun.txt, replace text

gen ID = _n
destring Q12, generate(Q12new)
destring Q13A, generate(Q13Anew)
destring Q13B, generate(Q13Bnew)
destring Q16, generate(Q16new)
destring Q38D1, generate(Q38D1new)
destring Q38D2, generate(Q38D2new)
destring Q38D3, generate(Q38D3new)
destring Q38D4, generate(Q38D4new)
destring Q70, generate(Q70new)
destring Q71, generate(Q71new)
destring Q73, generate(Q73new)
destring Q74, generate(Q74new)
destring Q75, generate(Q75new)
destring Q76, generate(Q76new)
destring Q77, generate(Q77new)
destring Q78, generate(Q78new)
destring Q81, generate(Q81new)
destring FAIT, generate(FAITnew)
clonevar voteprov = Q12new
label define voteprov 1 "Liberals" 2 "PCs" 3 "NDP" 5 "Greens"
label values voteprov voteprov
gen libchoice = 0
replace libchoice = 1 if voteprov==1
gen pcchoice = 0
replace pcchoice = 1 if voteprov==2
gen ndpchoice = 0
replace ndpchoice = 1 if voteprov==3
gen greenchoice = 0
replace greenchoice = 1 if voteprov==5

clonevar voteref = Q13Bnew
clonevar voteref2cat = voteref
replace voteref2cat = . if voteref>2
replace voteref2cat = voteref2cat - 1
clonevar voteref3cat = Q13Bnew
replace voteref3cat = 3 if Q13Anew>2 | Q13Bnew>2 | Q13Bnew==.
gen newspaper = 0
replace newspaper = 1 if Q16new==3

*Take out those who say they voted but won't say which way:
replace voteref3cat = . if Q13Bnew==9
label define voteref3cat 1 "no" 2 "yes" 3 "abstain" 
label values voteref3cat voteref3cat
gen abstain = voteref3cat
replace abstain = 0 if voteref3cat<3
replace abstain = 1 if voteref3cat==3 
clonevar interestprovorig = Q14
gen interestprov = interestprovorig
replace interestprov = 0 if interestprovorig>10
replace interestprov = interestprov/10
clonevar interestpolorig = Q15
gen interestpol = interestpolorig
replace interestpol = 0 if interestpolorig>10
replace interestpol = interestpol/10
gen libtherm = Q28/100
gen pctherm = Q29/100
gen ndptherm = Q30/100
gen greentherm = Q32/100
clonevar leaner = Q73new
clonevar votefed = Q74new
clonevar birthyr = Q75new
clonevar gender = Q76new
gen female = gender - 1
clonevar educorig = Q77new
gen educ = educorig
replace educ = . if educorig>11
replace educ = (educ - 1)/10
clonevar incomeorig = Q78new
gen income = incomeorig
replace income = 1 if incomeorig>10
replace income = (income - 1)/9
gen incmissing = 0
replace incmissing = 1 if incomeorig>10
clonevar churchattendorig = Q81new
gen churchattend = churchattendorig
replace churchattend = 5 if churchattendorig>5
replace churchattend = (5 - churchattend)/4
clonevar denom = FAITnew
gen ageorig = AGE
gen age = (ageorig -18)/82
gen age4cat = .
replace age4cat = 1 if AGE<31 
replace age4cat = 2 if AGE<46 & AGE>30
replace age4cat = 3 if AGE<61 & AGE>45
replace age4cat = 4 if AGE>60

clonevar weight = POND
* gen PID vars
clonevar partypref = Q70new
replace partypref = 4 if partypref==5
*Now make none, DK and other = Independents.
*Drop refused.
replace partypref = 5 if partypref>5
replace partypref = . if Q70new==99
label define partypref 1 "Liberal" 2 "PC" 3 "NDP" 4 "Green" 5 "Independent"
label values partypref partypref
clonevar partystrengthorig = Q71new
gen indep =0
replace indep = 1 if partypref >5 & partypref!=.
gen partystrength = partystrengthorig
*set refused and DK for strength to lowest strength for identifiers ("not very close").
replace partystrength = 3 if partystrengthorig ==8 | partystrengthorig ==9
replace partystrength = 5 - partystrength if partystrength != .
*Now put in leaners
replace partystrength = 1 if leaner <90
replace partystrength = 0 if partystrength==.  
label define partystrength 0 "none" 1 "lean" 2 "weak" 3 "medium" 4 "strong"
label values partystrength pidstrengthorig

gen pidlib = 0 if partypref!=.
replace pidlib = partystrength/4 if partypref==1 | leaner==1
gen pidpc = 0 if partypref!=.
replace pidpc = partystrength/4 if partypref==2 | leaner==2
gen pidndp = 0 if partypref!=.
replace pidndp = partystrength/4 if partypref==3 | leaner==3
gen pidgreen = 0 if partypref!=.
*Green leaners are 5, while they are 4 on partypref; hence:
replace pidgreen = partystrength/4 if partypref==4 | leaner==5
*We already have indep, which is for those who say they are Independent.
*Following variable excludes from indep those who lean.
gen indeppure = 0 if partypref!=.
replace indeppure = 1 if pidlib==0 & pidpc==0 & pidndp==0 & pidgreen==0

*now generate "big PID" for Lib/PC and "small" for NDP/Greens:
gen pidbig = pidlib + pidpc
gen pidsmall = pidndp + pidgreen

*Code correct answers on which side each party favored:
gen libknow = 0
replace libknow = 1 if Q38D1new==2
gen pcknow = 0
replace pcknow = 1 if Q38D2new==2
gen ndpknow = 0
replace ndpknow = 1 if Q38D3new==1
gen greenknow = 0
replace greenknow= 1 if Q38D4new==1
* generate party position knowledge score on 0-1 scale:
gen partiesknow = (libknow +pcknow +ndpknow +greenknow)/4
* take out Liberals in knowledge score
gen partiesclearknow = (pcknow +ndpknow +greenknow)/3
gen pidlibprtsknow = pidlib*partiesknow
gen pidpcprtsknow = pidpc*partiesknow
gen pidndpprtsknow = pidndp*partiesknow
gen pidgreenprtsknow = pidgreen*partiesknow
gen nopidprtsknow = indeppure*partiesknow
*next one defined without leaners
gen ownpartyknow = 0
replace ownpartyknow = 1 if libknow==1 & partypref==1
replace ownpartyknow = 1 if pcknow==1 & partypref==2
replace ownpartyknow = 1 if ndpknow==1 & partypref==3
replace ownpartyknow = 1 if greenknow==1 & partypref==4

*Now with leaners:
gen ownpartyknowbroad = ownpartyknow
replace ownpartyknowbroad = 1 if libknow==1 & pidlib==.25
replace ownpartyknowbroad = 1 if pcknow==1 & pidpc==.25
replace ownpartyknowbroad = 1 if ndpknow==1 & pidndp==.25
replace ownpartyknowbroad = 1 if greenknow==1 & pidndp==.25

*Create PID vs. know own party interactions (Lib one possibly less meaningful).
*Note that leaners are included.
gen pidlibknow = pidlib*libknow
gen pidpcknow = pidpc*pcknow
gen pidndpknow = pidndp*ndpknow
gen pidgreenknow = pidgreen*greenknow

*Also provincial vote choice with know:
gen libchoiceknow = libchoice*libknow
gen pcchoiceknow = pcchoice*pcknow
gen ndpchoiceknow = ndpchoice*ndpknow
gen greenchoiceknow = greenchoice*greenknow

gen unity = 1
}
*How are party positions perceived?
*First, proportion DK virtually identical across all four parties
tab Q38D1new  [aweight=weight]
tab Q38D2new  [aweight=weight]
tab Q38D3new  [aweight=weight]
tab Q38D4new  [aweight=weight]

*Now for raw N's:
tab Q38D1new 
tab Q38D2new 
tab Q38D3new  
tab Q38D4new 

*Second, all but Libs are perceived pretty clearly
tab Q38D1new if Q38D1new<3 [aweight=weight]
tab Q38D2new if Q38D2new<3  [aweight=weight]
tab Q38D3new if Q38D3new<3  [aweight=weight]
tab Q38D4new if Q38D4new<3  [aweight=weight]

corr libtherm pidlib [aweight=weight]
corr pctherm pidpc [aweight=weight]
corr ndptherm pidndp [aweight=weight]
corr greentherm pidgreen [aweight=weight]

*Did newspaper reading matter?

tab voteref2cat newspaper [aweight=weight], col


*For figure 9, first without leaners--used for fig. 9:
bysort partypref: tab voteref2cat ownpartyknow [aweight = weight], col
*Now to get N's:
bysort partypref: tab voteref2cat ownpartyknow, col

*Now with leaners (adds a few observations)--not used:
tab voteref2cat libknow [aweight=weight] if pidlib>.2, col
tab voteref2cat pcknow [aweight=weight] if pidpc>.2, col
tab voteref2cat ndpknow [aweight=weight] if pidndp>.2, col
tab voteref2cat greenknow [aweight=weight] if pidgreen>.2, col
*Now the pure Independents:
tab voteref2cat  [aweight=weight] if partypref==5 & leaner >5
*Find what total number of cases in these four crosstabs should be (total checks):
tab voteref2cat, miss
*Find N's for each of previous tabs:
tab voteref2cat libknow if pidlib>.2, col
tab voteref2cat pcknow if pidpc>.2, col
tab voteref2cat ndpknow if pidndp>.2, col
tab voteref2cat greenknow if pidgreen>.2, col
*Now the pure Independents:
tab voteref2cat if partypref==5 & leaner >5

*Two runs to be used, and check third:
probit voteref2cat educ age i.libchoice i.pcchoice i.ndpchoice i.greenchoice i.newspaper [pweight=weight]
fitstat
margins, dydx(*) post

probit voteref2cat educ age pidlib  pidpc  pidndp  pidgreen i.newspaper [pweight=weight]
fitstat
margins, dydx(*) post

gen ndpnewspaper = pidndp*newspaper

probit voteref2cat educ age pidlib  pidpc pidndp  pidgreen i.newspaper ndpnewspaper [pweight =weight]
fitstat
margins, dydx(*) post

*Check on income and gender:

probit voteref2cat educ age libchoice pcchoice ndpchoice greenchoice i.newspaper income i.incmissing i.female [pweight=weight]
fitstat
margins, dydx(*) post

probit voteref2cat educ age pidlib  pidpc  pidndp  pidgreen i.newspaper income i.incmissing i.female [pweight=weight]
fitstat
margins, dydx(*) post

*Following not used due to endogeneity of know variables:
probit voteref2cat age educ pidlib pidpc pidpcknow pidndp pidndpknow pidgreen pidgreenknow   [pweight=weight]
fitstat
predict predsorig1, xb

probit voteref2cat age educ pidlib pidpc pidpcknow pidndp pidndpknow pidgreen pidgreenknow newspaper  [pweight=weight]
fitstat


* GRAPHS

*combine these to make fig 8 in text:
graph bar (mean) voteref2cat [aweight = weight] if voteprov<6, over(voteprov) ytitle(fraction yes) ti(Ontario 2007) subti(Stephenson-Tanguay survey)
graph export fig4.pdf,  logo(off) replace
graph save fig4, replace

*partypref already <6; others missing, hence:

graph bar (mean) voteref2cat [aweight = weight] , over(partypref) ti(Ontario 2007 Referendum Vote by Party ID) ytitle(fraction yes) 
graph export fig5.pdf,  logo(off) replace
graph save fig5, replace

*Ns for above crosstabs:
tab voteref2cat voteprov if voteprov<6
tab voteref2cat partypref 

*Check that graphs are correct:
svyset [pweight=weight]
svy: tab voteref2cat voteprov if voteprov<6, col pearson
svy: tab voteref2cat partypref , col pearson
* For N's (check that same with svy):
tab voteref2cat voteprov if voteprov<6
tab voteref2cat partypref 

*Check partisan voting for those who reported voted (for one of the 4 parties) and get Ns:
svy: tab voteref2cat partypref if voteprov<6, col pearson
tab voteref2cat partypref if voteprov<6

log close
